﻿using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Ria;
using System.Windows.Threading;
using TheOliver.DigitalSignage.Client.Interfaces;

namespace CeBIT2010
{
    public partial class CebitAgenda : UserControl, IAgenda
    {
        public CebitAgenda()
        {
            InitializeComponent();
        }

        private int C_ItemTime = 100;
        private int C_WaitTime = 5000;
        
        private void ShowItems()
        {
            if (Configuration.AllSlots != null)
            {
                //int i = 0;
                int minGap = 0;
                DispatcherTimer waitTimer;
                
                IOrderedEnumerable<Web.AllSlots> allSlotsOrdered;

                // TODO: Show only upcoming Sessions

                // Get Session for selected Date
                allSlotsOrdered = Configuration.AllSlots
                    .Where(s => s.SlotDate == Configuration.CurrentDateTime)
                    .OrderBy(s => s.SlotTime);
                
                // If no sessions available get next available date
                DateTime firstDay = DateTime.Today;

                if (allSlotsOrdered.Count() == 0)
                {
                    var firstDaySlots = Configuration.AllSlots
                        .Where(s => s.SlotDate >= Configuration.CurrentDateTime)
                        .OrderBy(s => s.SlotDate);
                    if (firstDaySlots.Count() > 0)
                    {
                        firstDay = firstDaySlots.First().SlotDate;
                    }
                }
                else
                {
                    firstDay = allSlotsOrdered.First().SlotDate;
                }

                allSlotsOrdered = Configuration.AllSlots
                    .Where(s => s.SlotDate == firstDay)
                    .OrderBy(s => s.SlotTime);

                CebitAgendaItem ce = new CebitAgendaItem();
                double maxHeight = Configuration.AllSlots.Count * (ce.Height + minGap);
                double pagesNeed = maxHeight / (_itemsPanel.ActualHeight);
                pagesNeed = Math.Ceiling(pagesNeed);

                int currentItem = 0;
                int currentPage = 1;
                int itemsPerPage = (int)(allSlotsOrdered.Count() / pagesNeed);
                int gap = (int)Math.Ceiling((_itemsPanel.ActualHeight - (itemsPerPage+1) * ce.Height) / (itemsPerPage+1));

                 int itemsOnPage = 0;
                 DispatcherTimer itemTimer = new DispatcherTimer();
                 itemTimer.Interval = new TimeSpan(0, 0, 0, 0, C_ItemTime);
                 itemTimer.Tick += (s, e) => 
                     {
                         itemTimer.Stop();
                         
                         if (currentPage <= pagesNeed && 
                             currentItem < allSlotsOrdered.Count())
                         {
                             if (itemsOnPage > itemsPerPage)
                             {
                                 // Wait a second, clean page and continue
                                 waitTimer = new DispatcherTimer();
                                 waitTimer.Interval = new TimeSpan(0, 0, 0, 0, C_WaitTime);
                                 waitTimer.Tick += (s1, e1) =>
                                     {
                                         waitTimer.Stop();
                                         _itemsPanel.Children.Clear();
                                         itemsOnPage = 0;
                                         currentPage++;
                                         itemTimer.Start();
                                     };
                                 waitTimer.Start();
                             }
                             else
                             {
                                 // Add Item
                                 var slot = allSlotsOrdered.ToArray()[currentItem];

                                 CebitAgendaItem item = new CebitAgendaItem();
                                 item.DataContext = slot;
                                 item.SetValue(Canvas.TopProperty, itemsOnPage * gap + itemsOnPage * item.Height);
                                 _itemsPanel.Children.Add(item);
                                 item.Width = _itemsPanel.ActualWidth;

                                 currentItem++;
                                 itemsOnPage++;
                                 itemTimer.Start();
                             }
                         }
                         else
                         {
                             // Last Item displayed
                             waitTimer = new DispatcherTimer();
                             waitTimer.Interval = new TimeSpan(0, 0, 0, 0, C_WaitTime);
                             waitTimer.Tick += (s1, e1) =>
                             {
                                 waitTimer.Stop();
                                 OnFinished();
                             };
                             waitTimer.Start();
                             return;
                         }
                     };
                 itemTimer.Start();

                
            }
            else
            {
                OnFinished();
            }
        }

        #region ISlide Members

        public event EventHandler Finished;
        private void OnFinished()
        {
            if (Finished != null)
            {
                Finished(this, EventArgs.Empty);
            }
        }

        public int DisplayDuration
        {
            get { return 0; }
        }

        #endregion

        #region IAgenda Members

        public void Start()
        {
            if (!this._contentLoaded)
            {
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = new TimeSpan(0, 0, 2);
                timer.Tick += (s, e) =>
                    {
                        timer.Stop();
                        Start();
                    };
                timer.Start();
            }
            else
            {
                ShowItems();
            }
            
        }

        #endregion

    }
}
